<template>
  <div>
    <zk-form
      :model="${modelName?uncap_first}FormNew"
      label-position="left"
      label-width="100px"
      :rules="${modelName?uncap_first}Rules"
      ref="${modelName?uncap_first}Form">
          <#list fields as field>
            <#if field.name != 'id' && field.type != 'Timestamp' && field.type != 'Date'> 
            <zk-form-item :label="$t('${moduleName}_${modelName?uncap_first}_${field.name}')" prop="${field.name}">
	        	<zk-input v-model="${modelName?uncap_first}FormNew.${field.name}"></zk-input>
		    </zk-form-item>
		    </#if>
		 </#list>
    <div class="dialog-button">
      <zk-button type="primary" @click="save">{{$t('common_sure')}}</zk-button>
    </div>
</div>
</template>
<script>
<#assign size1 = 0>
 <#list fields as field>
  <#if field.name != 'id' && field.type != 'Timestamp' && field.type != 'Date'>
  	<#assign size1 = size1 + 1>
  </#if>
 </#list>
import * as ${modelName}Api from 'src/api/${modelName?uncap_first}.js'
export default {
  name: 'edit${vueModel?cap_first}',
  props: {
    currentModelForm: {
      type: Object,
      default () {
        return {
        <#assign num1 = 1>
          <#list fields as field>
          <#if field.name != 'id' && field.type != 'Timestamp' && field.type != 'Date'> 
          ${field.name}: ''<#if num1 != size1>,</#if>
			<#assign num1 = num1 + 1>
          </#if>
		 </#list>
        }
      }
    },
    isEdit: Boolean
  },
  data() {
    return {
      ${modelName?uncap_first}FormNew: {},
      defaultValue: {
       <#assign num2 = 1>
          <#list fields as field>
          <#if field.name != 'id' && field.type != 'Timestamp' && field.type != 'Date'> 
        ${field.name}: ''<#if num2 != size1>,</#if>
			<#assign num2 = num2 + 1>
        </#if>
		 </#list>
      },
      ${modelName?uncap_first}Rules: {
      }
    }
  },
  mounted() {
    this.${modelName?uncap_first}FormNew = JSON.parse(JSON.stringify(this.isEdit ? this.${modelName?uncap_first}Form : this.defaultValue))
  },
  watch: {
    isEdit(v, o) {
      if (!v) {
        this.$refs.${modelName?uncap_first}Form.resetFields()
        Object.assign(this.${modelName?uncap_first}FormNew, this.defaultValue)
      }
      console.log(v, this.${modelName?uncap_first}Form)
    },
    currentModelForm (v) {
      this.${modelName?uncap_first}FormNew = JSON.parse(JSON.stringify(v))
    }
  },
  methods: {
    save() {
      this.$refs.${modelName?uncap_first}Form.validate((valid) => {
        if (valid) {
          // 校验通过
          if (this.isEdit) {
            ${modelName}Api.update(this.${modelName?uncap_first}FormNew).then((response) => {
              if (response.body.code === '00000000') {
                this.$message({ message: this.$t('common_edit_success'), type: 'success' })
                this.$parent.close()
                this.$emit('edit-change', false)
                this.$parent.$emit('query')
              } else {
                this.$message({ message: response.body.message, type: 'error' })
              }
            }, (response) => {
              this.$message({ message: this.$t('common_op_faild'), type: 'error' })
            })
          } else {
            ${modelName}Api.save(this.${modelName?uncap_first}FormNew).then((response) => {
              if (response.body.code === '00000000') {
                this.$message({ message: this.$t('common_add_success'), type: 'success' })
                this.$emit('edit-change', true)
                this.$parent.close()
                this.$parent.$emit('query')
              } else {
                this.$message({ message: response.body.message, type: 'error' })
              }
            }, (response) => {
              this.$message({ message: this.$t('common_op_faild'), type: 'error' })
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
